package com.base.log;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author:sjb
 * @CreateTime:2024-08-20
 * @Description: 全局日志类
 * @Version：1.0
 */
public class Logger {

    private FileWriter fileWriter;

    private static final Logger instance;

    static {
        try {
            instance = new Logger();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private Logger() throws IOException {
        File file = new File("log.txt");
        fileWriter = new FileWriter(file,true);
    }

    public static Logger getInstance(){
        return instance;
    }

    public void log(String message,Object... args) throws IOException {
        for (Object arg : args) {
            message = message.replaceFirst("\\{\\}", arg.toString());
        }
        fileWriter.write(message);
        fileWriter.write("\n");
        fileWriter.flush();
    }

    public void close() throws IOException {
        fileWriter.close();  // 关闭 FileWriter
    }
}
